import Vue from 'vue'
import VueRouter from 'vue-router'
import index from '../views/index/index'
import courseDetail from "../views/courseDetail/courseDetail";
import information from "../views/personalInfomation/information";
import vuex from "../views/vuex";
import {getPersonal} from "../../api/personalCenter";
Vue.use(VueRouter)

const routes = [
  {
    path:"/",
    redirect:"/index"
  },
  {
    path:'/index',
    component:index
  },
  {
    path:"/courseDetail",
    component:courseDetail
  },
  {
    path:"/information",
    component:information,
    meta:{
      needLogin:true
    }
  },
  {
    path:"/vuex",
    component: vuex

  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
router.beforeEach((to,from,next)=>{
      if(to.meta.needLogin){//判断即将跳转的页面是否需要登录
          getPersonal().then(res=>{//请求当前登录的账号的个人信息
              if(res.code==0){//如果请求成功说明已经登录
                  next();//登录的话直接跳转
              }else{
                  next({path:'/index'})//没登录就跳转到首页
              }
          })
      }else{
        next();//跳转的玉面不需要登录的话直接跳转
      }
})
export default router
